package co.edu.usbcali.lidis.vista;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;

import co.edu.usbcali.lidis.modelo.Usuario;

@SuppressWarnings("serial")
public class PantallaInicio extends JFrame {

	public PantallaInicio() {
		setUndecorated(true);
		initComponents();
		String path = System.getProperty("user.dir");
		Panel p = new Panel(path + "/imagenes/fondoInicio.png");
		System.out.println(p);
		getContentPane().add(p, BorderLayout.CENTER);
		this.pack();
		setSize(510, 302);
	}

	private void initComponents() {

		btnIngresar = new javax.swing.JButton();
		jButton3 = new javax.swing.JButton();
		txtUsuario = new javax.swing.JTextField();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		txtContrasena = new javax.swing.JPasswordField();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Inicio de Sesión");
		setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
		setForeground(java.awt.Color.white);
		setIconImage(getIconImage());
		setPreferredSize(new java.awt.Dimension(510, 302));

		btnIngresar.setMnemonic('1');
		btnIngresar.setText("Ingresar");

		btnIngresar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnIngresarActionPerformed(evt);
			}
		});

		jButton3.setMnemonic('s');
		jButton3.setText("Salir");
		jButton3.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton3ActionPerformed(evt);
			}
		});

		jLabel1.setText("Usuario: ");

		jLabel2.setText("Contraseña:");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		layout.setHorizontalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(46)
								.addGroup(
										layout.createParallelGroup(
												Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.LEADING,
																				false)
																				.addComponent(
																						jLabel2,
																						GroupLayout.DEFAULT_SIZE,
																						94,
																						Short.MAX_VALUE)
																				.addComponent(
																						jLabel1))
																.addPreferredGap(
																		ComponentPlacement.UNRELATED)
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.LEADING,
																				false)
																				.addComponent(
																						txtUsuario)
																				.addComponent(
																						txtContrasena,
																						GroupLayout.PREFERRED_SIZE,
																						142,
																						GroupLayout.PREFERRED_SIZE)))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(44)
																.addComponent(
																		btnIngresar)
																.addGap(18)
																.addComponent(
																		jButton3)))
								.addContainerGap(202, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(74, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(jLabel1)
												.addComponent(
														txtUsuario,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(jLabel2)
												.addComponent(
														txtContrasena,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addGap(44)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(btnIngresar)
												.addComponent(jButton3))
								.addGap(71)));
		getContentPane().setLayout(layout);

		setSize(new java.awt.Dimension(516, 342));
		setLocationRelativeTo(null);
	}// </editor-fold>

	@SuppressWarnings("deprecation")
	private void btnIngresarActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			if (txtUsuario.getText().trim().equals("") == true) {
				throw new Exception("Por favor ingrese un usuario.");
			}
			if (txtContrasena.getText().trim().equals("") == true) {
				throw new Exception("Por favor ingrese una contraseña.");
			}

			Usuario usu = new Usuario();
			usu.setUsuUsuario(txtUsuario.getText());
			usu.setUsuPassword(txtContrasena.getText());

			int idUsuario = BusinessDelegate.getInstance().inicioSesion(usu);

			if (idUsuario != 0) {
				PantallaMenu pantallaMenu = new PantallaMenu(idUsuario);
				pantallaMenu.setSize(400, 200);
				pantallaMenu.setVisible(true);
				pantallaMenu.setLocationRelativeTo(null);
				dispose();
				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			} else {
				throw new Exception("Datos inválidos.");
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
		System.exit(0);
	}

	@Override
	public Image getIconImage() {
		String path = System.getProperty("user.dir");
		System.out.println(path);
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Image retValue = toolkit.getImage(path + "/imagenes/icono.png");
		return retValue;
	}

	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new PantallaInicio().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JButton btnIngresar;
	private javax.swing.JButton jButton3;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JPasswordField txtContrasena;
	private javax.swing.JTextField txtUsuario;
	// End of variables declaration
}
